// pages/setAlarm/setAlarm.js
import { ble_send_date} from "../../utils/util.js";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    label_value: '周末闹钟',
    activeName: '0',


    currentDate: '12:30', //选择框默认定位时间
    alarm_show: false,

    alarm_id:'',
    alarm_tm_hour : '', //闹钟一
    alarm_tm_min : '',
    alarm_state : '',
    alarm_play_time: '',//每天
    alarm_ring_tone : '',
    alarm_over_sleeping : '',
    

    date_name: [{
        id: 64,
        name: '日',
        sta: 0,
      },
      {
        id: 32,
        name: '一',
        sta: 0,
      },
      {
        id: 16,
        name: '二',
        sta: 1,
      },
      {
        id: 8,
        name: '三',
        sta: 0,
      },
      {
        id: 4,
        name: '四',
        sta: 1,
      },
      {
        id: 2,
        name: '五',
        sta: 0,
      },
      {
        id: 1,
        name: '六',
        sta: 0,
      },
    ],
    ring_list: [{
        id: 0,
        name: "星空"
      },
      {
        id: 1,
        name: "丝绸"
      },
      {
        id: 2,
        name: "轻音乐"
      },
      {
        id: 3,
        name: "钟响磬鸣‌"
      },
      {
        id: 4,
        name: "卡农"
      },
    ]
  },
  //点击星期选择
  onSetPlayTime(e){
    console.log(e.target.id);
    let timerDate;
    if (e.target.id & this.data.alarm_play_time) {
      timerDate = this.data.alarm_play_time ^ e.target.id
    }else{
      timerDate = this.data.alarm_play_time | e.target.id
    }
    this.setData({alarm_play_time: timerDate})
    //更新 星期列表
    this.update_alarm_play_tiem();
    console.log(this.data.alarm_play_time);
  },

  //处理星期显示
  update_alarm_play_tiem() {
    const num = this.data.alarm_play_time;
    let d_name = this.data.date_name;
    for (let bit = 0; bit < 7; bit++) { //动态更新页面
      // 判断第bit位是否为1
      const isSet = (num & (1 << bit)) !== 0;
      //console.log(`第 ${bit} 位: ${isSet ? '高 (1)' : '低 (0)'}`);
      d_name[6 - bit].sta = isSet ? 1 : 0; ////高位就激活
      this.setData({
        date_name: d_name
      })
    }
  },

  //打开闹钟设置 弹出层
  onSetAlarm() {
    this.setData({
      alarm_show: true
    });
  },
  //确认闹钟选择时间
  alarm_confirm({detail}) {
    let timer = detail.split(':');
    console.log(timer);
    this.setData({
      alarm_show: false,
      alarm_tm_hour: timer[0],
      alarm_tm_min:timer[1],
    });
  },

  onClose() {
    this.setData({
      alarm_show: false
    });
  },

  alarm_cancel() {
    this.setData({
      alarm_show: false
    });
  },

  onInput(event) {////选择框默认定位时间
    this.setData({
      currentDate: event.detail,
    });
  },

  //是否打开贪睡
  onChange_ring_strong({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    console.log(detail);
    this.setData({
      alarm_over_sleeping: this.data.alarm_over_sleeping ^= 1,
    });
  },

  //打开折叠
  onChange_ring(event) {
    this.setData({
      activeName: event.detail,
    });
  },

  //选择铃声
  onSetRing(e) {
    let { music_ring } = app.store.getState();
    console.log(e.target.id);
    this.setData({
      alarm_ring_tone: e.target.id,
    });
    app.store.setState({ music_ring:e.target.id});
    ble_send_date(23);
  },

  //保存闹钟
  onSaveAlarm() {
    let { alarm_list } = app.store.getState();
    alarm_list[this.data.alarm_id - 1].alarm_tm_hour1 = Number(this.data.alarm_tm_hour);
    alarm_list[this.data.alarm_id - 1].alarm_tm_min1 = Number(this.data.alarm_tm_min);
    alarm_list[this.data.alarm_id - 1].alarm_state1 = this.data.alarm_state;
    alarm_list[this.data.alarm_id - 1].alarm_play_time1 = this.data.alarm_play_time;
    alarm_list[this.data.alarm_id - 1].alarm_ring_tone1 = this.data.alarm_ring_tone;
    alarm_list[this.data.alarm_id - 1].alarm_over_sleeping1 = this.data.alarm_over_sleeping;
    app.store.setState({ alarm_list});
    ble_send_date(this.data.alarm_id);
    wx.switchTab({
      url: '../index/index',
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //赋值闹钟数据到页面
    let setAlarmObj = JSON.parse(options.alarmObj)
    console.log(setAlarmObj);
    this.setData({
      alarm_id:setAlarmObj.id,
      alarm_tm_hour: setAlarmObj.alarm_tm_hour1 < 10 ? '0' + setAlarmObj.alarm_tm_hour1 : setAlarmObj.alarm_tm_hour1,
      alarm_tm_min : setAlarmObj.alarm_tm_min1 < 10 ? '0' + setAlarmObj.alarm_tm_min1 : setAlarmObj.alarm_tm_min1,
      alarm_state : setAlarmObj.alarm_state1,
      alarm_play_time: setAlarmObj.alarm_play_time1,//每天
      alarm_ring_tone : setAlarmObj.alarm_ring_tone1,
      alarm_over_sleeping : setAlarmObj.alarm_over_sleeping1,
    })
    let timer1 = setAlarmObj.alarm_tm_hour1 < 10 ? '0' + setAlarmObj.alarm_tm_hour1 : setAlarmObj.alarm_tm_hour1;
    let timer2 = setAlarmObj.alarm_tm_min1 < 10 ? '0' + setAlarmObj.alarm_tm_min1 : setAlarmObj.alarm_tm_min1;
    let t = `${timer1}:${timer2}`;
    console.log(t);
    this.setData({currentDate : t})
    this.update_alarm_play_tiem();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})